<?php 
	function settings_upload_avatar($files,$uploadDir,$flag=1)
	{
		//flag? 1 => images
		//		2 => video
		//		3 => file
		$size=$files['size'];
		if($size>2048576000)
		{
			echo "file_biger";
			unlink($files['tmp_name']);
			exit;
		}
		
		$filename = stripslashes($files['name']);
		$extension = strtolower(get_file_extension($filename));
		switch ($flag){
		case '1':
			$ischeck = isImage($extension);
			break;
		case '2':
			$ischeck = isVideo($extension);	
			break;
		case '3':
			$ischeck = isFile($extension);
			break;
		case '4':
			$ischeck = isFlash($extension);
			break;
		
		}

		if($ischeck==1)
		{
			$name_temp =str_replace( $extension ,'',$filename); 
			$name =date("dmyHis")."_".removeSign($name_temp).".".$extension;
			//$file_ext = $name.'.'.$extension;
			$dirfilename = $uploadDir.$name;
			if (move_uploaded_file($files['tmp_name'], $dirfilename))
			{
				return $name ;
				echo json_encode($data) ;
			}else{
				$error ="Gặp sự cố khi tải lên"; 
				return false;
			}
		}else{
			return '-1';
		}
           
	}
	function removeSign($str) 
	{  
		$str = strip_tags($str);
		$str = mb_strtolower($str,'utf-8');
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);  //|á|à|ả|ã|ạ|ấ|ầ|ẩ|ẫ|ậ|ắ|ằ|ẳ|ẵ|ặ
		$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);  
		$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);  
		$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);  
		$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);  
		$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);  
		$str = preg_replace("/(đ)/", 'd', $str);
		$str = preg_replace("/(:|;)/","",$str);
		$str = preg_replace("/(́|̀|̉|̃|̣)/","",$str);
		$str = str_replace("_","",$str);
		$str = str_replace("?","",$str);
		$str = str_replace('"',"",$str); 
		$str = str_replace(")","",$str); 
		$str = str_replace("/","",$str);
		$str = str_replace("%","",$str);
		$str = str_replace("&","",$str);
		$str = str_replace("–","",$str);
		$str = str_replace("_","",$str);
		$str = str_replace(".","",$str);
		$str = str_replace(",","",$str);
		$str = str_replace("-","",$str);
		$str = str_replace("(","",$str);
		$str = str_replace("!","",$str); 
		$str = str_replace(" ","-",$str); 
		$str = str_replace("----","-",$str);
		$str = str_replace("---","-",$str);
		$str = str_replace("--","-",$str);
		$str = preg_replace(array('/[^a-zA-Z0-9 -]/'), array(''), $str) ;
		return $str; 
 }
 
	
	function toMBytes($bytes,$decimal=2) {
		$kb = ($bytes * 8) / 1024;
		$mb = ($kb /1024) / 8;
		return round($mb,$decimal);
	}
	
	function toKBytes($bytes,$decimal=2) {
		$kb = ($bytes * 1) / 1024;
		return round($kb,$decimal);
	}

/*-----------------------------------------------------------------------*
* Function: toBytes
* Parameter: Mega bytes
* Constants:  UPLOAD_SIZE_MBYTES | UPLOAD_SIZE_BYTES
* Return: file size 
*-----------------------------------------------------------------------*/

	function toBytes($mb) {
		$bytes = ((($mb * 8) * 1024) * 1024) / 8;
		return $bytes;
	}
	function isImage($str) {

		if(preg_match("/jpg|png|gif|jpeg/",$str)) return 1;
		return 0;
	}
    function isFlash($str) {

		if(preg_match("/swf/",$str)) return 1;
		return 0;
	}
	function isVideo($str) {

		if(preg_match("/wmv|mpg|mp4|mov|avi|flv/",$str)) return 1;
		return 0;
	}
	function isMusic($str) {

		if(preg_match("/wma|wav|mp3|asf/",$str)) return 1;
		return 0;
	}
	function isFile($str) {
		if(preg_match("/doc|zip|rar|docx|xlsx|pptx|pdf|xls/",$str)) return 1;
		return 0;
	}
	function get_file_extension($file_name) {
		return substr(strrchr($file_name,'.'),1);
	}
  	
	
?>